package org.hawksee.javase;

import java.awt.image.BufferedImage;

import org.hawksee.core.data.BaseMonochromeImage;

public class BufferedMonochromeImage extends BaseMonochromeImage
{
  private BufferedImage bufferedImage;
  public BufferedMonochromeImage(BufferedImage bufferedImage)
  {
    this.bufferedImage = bufferedImage;
  }
  public int getHeight()
  {
    return this.bufferedImage.getHeight();
  }
  public byte getIntensity(int x, int y)
  {
    if ((x < 0) || (y < 0) || (x >= this.getWidth()) || (y >= this.getHeight())) {
      return -1;
    }
    int rgb = this.bufferedImage.getRGB(x, y);
    byte red = (byte)((rgb & 0x00FF0000) >> 16);
    byte green = (byte)((rgb & 0x0000FF00) >> 8);
    byte blue = (byte)(rgb & 0x000000FF);
    
    return (byte)((red + green + blue) / 3.0);
  }
  public BufferedImage getImage()
  {
    return this.bufferedImage;
  }
  public int getWidth()
  {
    return this.bufferedImage.getWidth();
  }
  public BufferedMonochromeImage getSubimage(int x, int y, int width, int height)
  {
    BufferedImage bufSubimage = this.bufferedImage.getSubimage(x, y, width, height);
    return new BufferedMonochromeImage(bufSubimage);
  }
}
